package com.kiven.utils

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.PopupChooserBuilder
import com.intellij.ui.awt.RelativePoint
import com.intellij.ui.components.JBList
import com.ruiyu.utils.showNotify
import java.awt.Dimension
import java.awt.MouseInfo
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection

object KUtil {
    fun showPopupChoose(list:Array<String>, runnable: (Int) -> Unit) {
        val jList = JBList<String>()
        jList.setListData(list)

        val jbPopup = PopupChooserBuilder<String>(jList)
            .setItemChoosenCallback{
                runnable(jList.selectedIndex)
            }.createPopup()
        jbPopup.size = Dimension(350, jbPopup.content.preferredSize.height + 30)
        jbPopup.show(RelativePoint(MouseInfo.getPointerInfo().location))
    }

    fun copy(project: Project, text: String) {
        val clip = Toolkit.getDefaultToolkit().systemClipboard
        clip.setContents(StringSelection(text), null)


        project.showNotify("已将代码复制到剪贴板")
    }
}